
@extends('layout.layer')

@section('layout.content')
<div class="row">
  <div class="col-xs-12">
    <form class="form-horizontal" id="role-update" autocomplete="off">
      <input type="hidden" name="_method" value="put" />
    {{ csrf_field() }}
    <div class="form-group">
        <label class="col-sm-2 control-label no-padding-right" for="name">  角色名称：</label>
        <label class="control-label no-padding-left b"> {{$role->name}} ({{$role->value}})</label>
    </div>
    <div class="form-group">
        <label class="col-sm-2 control-label no-padding-right" for="name"> <b><span class="red">*</span> 权限：</b></label>
        <div class="col-sm-10 no-padding-left">
          @foreach($permissions as $parent)
          <div class="checkbox">
            <label class="no-padding-left">
              <span class="lbl" ><b> {{$parent->title}} </b></span>
            </label>
            @foreach ($parent['children'] as $permission)
              <label class="">
                <input name="permission" type="checkbox" value="{{$permission->id}}" class="ace ace-checkbox-2"
                @foreach($role->permissions as $role_permission)
                {{ $role_permission->id == $permission->id ? 'checked':'' }}
                @endforeach
                />
                <span class="lbl no-padding-left"> {{$permission->title}}</span>
              </label>
              @if( count($permission->children)>0 )
              （
                @foreach($permission->children as $children)
                <label class="">
                  <input name="permission" type="checkbox" value="{{$children->id}}" class="ace ace-checkbox-2"
                  @foreach($role->permissions as $role_permission)
                  {{ $role_permission->id == $children->id ? 'checked':'' }}
                  @endforeach
                  />
                  <span class="lbl no-padding-left"> {{$children->title}}</span>
                </label>
                @endforeach
               ）
              @endif
            @endforeach
          </div>
          @endforeach
        </div>
    </div>

    <hr />
    <div class="form-group">
      <div class="col-xs-12 center">
        <button type="button" class="btn btn-primary submit">
          <i class="ace-icon fa fa-check"></i>
          <span class="">确定</span>
        </button>
        <button type="button" class="btn btn-grey cancel" style="margin-left: 10px;">
          <i class="ace-icon fa fa-close"></i>
          <span class="">取消</span>
        </button>
      </div>
    </div>

  </form>
  </div>
</div>

@stop

@section('javascript')
<script type="text/javascript">

  $(function() {

    $('.submit').on('click', function() {

      var permissions = new Array();

      $("input[name='permission']:checked").each(function(){
        permissions.push($(this).val());
      });
      
      var uri = '{{route('system.role.update', $role->id)}}';

      $.post(uri, {
        '_token':$("input[name='_token']").attr('value'),
        '_method': $("input[name='_method']").attr('value'),
        'permissions': permissions,        
        'is_submit': true
      }, function(data) {
        console.log(data);
        layer.msg(data.message, {time: 2000,shift: -1}, function () {
          if (data.status === true) {
            parent.location.reload();
            parent.layer.close(parent.layer.index);
          }
        });
      },'json').error(function(data){
        layer.msg(data.responseJSON.message);
      });

    });


    $('.cancel').on('click', function() {
      parent.layer.closeAll();
    });

  });

</script>
@stop